package com.sansecy.video;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


public class GestureController extends VolumeBrightnessController {
    private final ImageView mVolumeView;
    private final LinearLayout mVolumeBrightnessLayout;
    private final ProgressBar mProgressBar;

    public GestureController(@NonNull Context context) {
        this(context, null);
    }

    public GestureController(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.fitness_volume_brightness_view, this, false);
        addView(view);
        mProgressBar = findViewById(R.id.progressBar);
        mProgressBar.setMax(100);
        mVolumeBrightnessLayout = findViewById(R.id.operation_volume_brightness);
        mVolumeBrightnessLayout.setVisibility(GONE);
        mVolumeView = (ImageView) findViewById(R.id.volumeView);
    }

    @Override
    protected void onVolumeChange(int maxVolume, int volume) {
        // 显示
        mVolumeBrightnessLayout.setVisibility(View.VISIBLE);
        mVolumeView.setVisibility(VISIBLE);
        mVolumeView.setImageResource(getVolumeImage(maxVolume, volume));
        // 变更进度条
        float currentProgress = volume / (float) maxVolume;
        mProgressBar.setProgress((int) (mProgressBar.getMax() * currentProgress));
    }

    @Override
    protected void onBrightnessChange(float maxScreenBrightness, float screenBrightness) {
        // 显示
        mVolumeBrightnessLayout.setVisibility(View.VISIBLE);
        mVolumeView.setVisibility(VISIBLE);
        mVolumeView.setImageResource(R.drawable.brightness);
        mProgressBar.setProgress((int) (mProgressBar.getMax() * screenBrightness));
    }

    private int getVolumeImage(int maxVolume, int index) {
        index = Math.round((float) index / maxVolume * 16f);
        int imageRes;
        switch (index) {
            case 0:
                imageRes = R.drawable.volume_0;
                break;
            case 1:
                imageRes = R.drawable.volume_1;
                break;
            case 2:
                imageRes = R.drawable.volume_1;
                break;
            case 3:
                imageRes = R.drawable.volume_2;
                break;
            case 4:
                imageRes = R.drawable.volume_3;
                break;
            case 5:
                imageRes = R.drawable.volume_4;
                break;
            case 6:
                imageRes = R.drawable.volume_5;
                break;
            case 7:
                imageRes = R.drawable.volume_6;
                break;
            case 8:
                imageRes = R.drawable.volume_7;
                break;
            case 9:
                imageRes = R.drawable.volume_8;
                break;
            case 10:
                imageRes = R.drawable.volume_9;
                break;
            case 11:
                imageRes = R.drawable.volume_10;
                break;
            case 12:
                imageRes = R.drawable.volume_11;
                break;
            case 13:
                imageRes = R.drawable.volume_12;
                break;
            case 14:
                imageRes = R.drawable.volume_13;
                break;
            case 15:
                imageRes = R.drawable.volume_14;
                break;
            case 16:
                imageRes = R.drawable.volume_15;
                break;
            default:
                imageRes = R.drawable.volume_0;
        }
        return imageRes;
    }

    @Override
    protected void onViewHide() {
        mVolumeBrightnessLayout.setVisibility(View.GONE);
    }

    @Override
    public void onProgressChange(float max, float progress) {
        mVolumeBrightnessLayout.setVisibility(View.VISIBLE);
        mVolumeView.setVisibility(VISIBLE);
        mVolumeView.setImageResource(R.drawable.progress);
        mProgressBar.setProgress((int) (mProgressBar.getMax() * (progress / max)));
    }
}
